package com.hong.test.util;

import lombok.SneakyThrows;

import java.io.*;

public class FileUtils {

    private static final String dirPath = "G:\\usr\\log";

    private static final String toTxtPath = "陕西跳动节拍网络科技有限公司.txt";

    private static final String content = "陕西跳动节拍网络科技有限公司";

    private static final int prefixNum = 10;

    private static final int suffixNum = 10;

    public static void setString(String content,String[] toArray){
        for (int i = 0; i < toArray.length; i++) {
            if (toArray[i] == null) {
                toArray[i] = content;
                return;
            }
        }
        for (int i = 1; i < toArray.length; i++) {
            toArray[i-1] = toArray[i];
            toArray[i] = content;
        }
    }

    public static void write(BufferedWriter bw,String[] toArray) throws IOException {
        for (int i = 0; i < toArray.length; i++) {
            if (toArray[i] != null) {
                bw.write(toArray[i]);
                bw.write("\n");
            }
        }
    }

    @SneakyThrows
    public static void main(String[] args) {
        // 获取文件夹
        File rootDirFile = new File(dirPath);
        if (!rootDirFile.exists()) {
            rootDirFile.mkdirs();
        }

        File toDirFile = new File(dirPath + File.separator + content);
        if (!toDirFile.exists()) {
            toDirFile.mkdirs();
        }

        File[] files = rootDirFile.listFiles();
        for (File file:files) {
            // 判断是否为文件夹
            if (file.isDirectory()){
                continue;
            }
            // 创建目标文件
            File toFile = new File(dirPath + File.separator + content, file.getName() + "_" + toTxtPath);
            if (!toFile.exists()){
                toFile.createNewFile();
            }
            BufferedWriter bw = new BufferedWriter(new FileWriter(toFile,true));


            // 以行为单位读取文件，常用于读面向行的格式化文件
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            String[] prefixArrary = new String[prefixNum];
            int line = 1;
            int write = 0;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                //是否包含指定字符串
                if (tempString.contains(content)){
                    if (write == 0){
                        write(bw,prefixArrary);
                    }
                    bw.write(tempString);
                    bw.write("\n");
                    // 显示行号
                    System.out.println("line " + line + ": " + tempString);
                    write = suffixNum;
                }
                if (write != 0){
                    if (write != suffixNum){
                        bw.write(tempString);
                        bw.write("\n");
                    }
                    write--;
                    if (write == 0){
                        bw.write("\n\n\n\n\n");
                    }
                }
                setString(tempString,prefixArrary);
                line++;
            }
            reader.close();
            bw.flush();
            bw.close();
            if (toFile.length() == 0){
                toFile.delete();
            }
        }
    }
}
